$(document).ready(
    function(){
        
        var UI = {};
        (function (UI) {
            
            UI.start = function(){
                
            };
            
        }(UI));
        var Helpers = {};
        (function (Helpers) {
            
            
        }(Helpers));
        
        var Actions = {};
        (function (Actions) {
            
            Actions.addBarsInsert = function(args) {
                var dfd = new $.Deferred();
                console.log("%c runSQL addBarsInsert", "color: green");
                $.ajax({
                    type: "POST",
                    url: "../resources/helpers/addBarsInsert.php",
                    data: args,
                    cache: false,
                    success: function(data){
                        console.log("%c Returned from addBarsInsert:","color: green");
                        console.log(data);
//                        data = JSON.parse(data);
//                        dfd.resolve(data);
                    }
                });
                return dfd.promise();
            };
            
            Actions.GetBarList = function(args) {
                var dfd = new $.Deferred();
                $.ajax({
                    type: "GET",
                    url: "../resources/helpers/addBarsGetBarList.php",
                    data: args,
                    cache: false,
                    success: function(data){
                        data = JSON.parse(data);
                        dfd.resolve(data);
                    }
                });
                return dfd.promise();
            };
            
        }(Actions));
        
        var Controller = {};
        (function (Controller) {
            function runSqlClicked(){
                var rowCount = $('#new-bar-grid tr').length;
                
                for(var i=1; i<rowCount; i++){
                    var row = $("#row-" + i);
                    if(row.html()){
                        var args ={
                            action          : "test",
                            name            : row.find("#name-"+i).html(),
                            description     : row.find("#description-"+i).html(),
                            postal_no       : row.find("#postal_no-"+i).html(),
                            place           : row.find("#place-"+i).html(),
                            address         : row.find("#address-"+i).html(),
                            cover_charge    : row.find("#cover_charge-"+i).html(),
                            beer_price      : row.find("#beer_price-"+i).html(),
                            age_limit       : row.find("#age_limit-"+i).html(),
                            user_id         : row.find("#user_id-"+i).html(),
                            status_id       : '1'
                        };
                        
                        $.when(Actions.addBarsInsert(args)).then(
                            function(){
                                
                            }
                        );
                    }
                    
                }
            }
            
            Controller.init= function(){
                $("#run-sql-btn").unbind("click").bind("click", runSqlClicked);
            };
            
        }(Controller));	
        
        UI.start();
        Controller.init();
    }
);